package com.msh.starter.id.generate.util;

import com.msh.frame.client.exception.BizException;
import com.msh.frame.interfaces.IIdGenerateServiceId;
import com.msh.frame.interfaces.IdGenerateable;
import com.msh.starter.id.generate.enums.IdGenerateLengthEnum;
import com.msh.starter.id.generate.enums.IdGenerateTypeEnum;
import com.msh.starter.id.generate.instance.DateIdGenerate;
import com.msh.starter.id.generate.instance.IdGenerate;

public class IdGenerateUtil {

    /**
     * 构建id生成器
     */
    public static IdGenerateable buildIdGenerate(IIdGenerateServiceId idGenerateServiceId, String typeCode, String lengthCode){
        if(IdGenerateTypeEnum.DEFAULT.getCode().equals(typeCode)){
            if(IdGenerateLengthEnum.ID_LENGTH_SHORT.getCode().equals(lengthCode)){
                return new IdGenerate(idGenerateServiceId, 1);
            }else if(IdGenerateLengthEnum.ID_LENGTH_MEDIUM.getCode().equals(lengthCode)){
                return new IdGenerate(idGenerateServiceId, 7);
            }else if(IdGenerateLengthEnum.ID_LENGTH_LONG.getCode().equals(lengthCode)){
                return new IdGenerate(idGenerateServiceId, 13);
            }
        }else if(IdGenerateTypeEnum.DATE.getCode().equals(typeCode)){
            if(IdGenerateLengthEnum.ID_LENGTH_SHORT.getCode().equals(lengthCode)){
                return new DateIdGenerate(idGenerateServiceId, 1);
            }else if(IdGenerateLengthEnum.ID_LENGTH_MEDIUM.getCode().equals(lengthCode)){
                return new DateIdGenerate(idGenerateServiceId, 1000);
            }else if(IdGenerateLengthEnum.ID_LENGTH_LONG.getCode().equals(lengthCode)){
                return new DateIdGenerate(idGenerateServiceId, 100000);
            }
        }
        throw new BizException(String.format("配置参数错误, typeCode: %s, lengthCode: %s.", typeCode, lengthCode));
    }
}
